<?php
namespace Admin\Controller;
use Common\Controller\CommonController;
use Common\Model\CaozuologModel;
class LoginController extends CommonController {
	//空操作
	public function _initialize(){
		
	}
	public function _empty(){
		header("HTTP/1.0 404 Not Found");
		$this->display('Public:404');
	}
	public function login(){
        $this->display();
    }
    //登录验证
    public function checkLogin(){
    	$username=trim(I('username'));
    	$pwd=trim(I('password'));
     	if(empty($username)||empty($pwd)){
     		$data = [
				'status'=>-1,
				'msg'=>'请输入完整'
     		];
     		$this->ajaxReturn($data);
     	}
     	$admin=M('Admin')->where("username='$username'")->find();
     	if(!$admin){
     		$data = [
     		'status'=>-2,
     		'msg'=>'未查询到此用户'
     				];
     		$this->ajaxReturn($data);
     	}
     	if($admin['status']!=1){
     		$data = [
     		'status'=>-5,
     		'msg'=>'此用户已经被冻结'
     				];
     		$this->ajaxReturn($data);
     	}
     	if($admin['password']!=md5($pwd)){
     		$data = [
     		'status'=>-3,
     		'msg'=>'密码不正确'
     				];
     		$this->ajaxReturn($data);
     	}
     	//生成token
     	$token = $admin['admin_id'].md5(time());
     	$back = [
     		'status'=>1,
     		'data'=>[
     				'token'=>$token,
     				'admin'=>$admin
     			],
     		];
     	M('Admin_token')->add(['uid'=>$admin['admin_id'],'token'=>$token]);
        $ip = $_SERVER["REMOTE_ADDR"];
		$arrs['ip'] = $ip;
		$arrs['add_time'] = time();
		$arrs['username'] = $username;
		M('Admin_log')->add($arrs);
		$M_LOG = new CaozuologModel();
		$content = '管理员登录后台';
		$M_LOG->addLog($admin['username'], $content);
    	$this->ajaxReturn($back);
    }

    //登出
    public function loginout(){
    	$_SESSION['admin_userid']=null;
    	$this->redirect('Login/login');
    }
     
}